//
//  HeadTopView.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/1/5.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "HeadTopView.h"

@interface HeadTopView ()

@property (nonatomic, strong) UILabel *title;
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *rightBtn;

@end

@implementation HeadTopView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupConfigureView];
    }
    return self;
}

- (void)setupConfigureView {
    self.backgroundColor = MainThemeColor;

    self.title = [[UILabel alloc] initWithFrame:CGRectMake(50, kStatusBarHeight, SCREEN_WIDTH - 100, 44)];
    self.title.textColor = [UIColor whiteColor];
    self.title.textAlignment = NSTextAlignmentCenter;
    self.title.font = [UIFont systemFontOfSize:18.0f];
    [self addSubview:self.title];
}

#pragma mark - setter
- (void)setText:(NSString *)text {
    self.title.text = text;
    _text = text;
}

- (void)setShowLeft:(BOOL)showLeft {
    _showLeft = showLeft;
    self.leftBtn.hidden = !showLeft;
}
- (void)setShowRight:(BOOL)showRight {
    _showRight = showRight;
    self.rightBtn.hidden = !showRight;
}

- (void)setLeftTitle:(NSString *)leftTitle {
    _leftTitle = leftTitle;
    [self.leftBtn setTitle:leftTitle forState:UIControlStateNormal];
}
- (void)setRightTitle:(NSString *)rightTitle {
    _rightTitle = rightTitle;
    [self.rightBtn setTitle:rightTitle forState:UIControlStateNormal];
    if ([SettingObject objectIsValid:rightTitle]) {
        CGRect frame = self.rightBtn.frame;
        frame.origin.x = frame.origin.x - 15;
        frame.size.width = frame.size.width + 15;
        self.rightBtn.frame = frame;
    }
}

- (void)setLeftImageName:(NSString *)leftImageName {
    _leftImageName = leftImageName;
    [self.leftBtn setImage:[UIImage imageNamed:leftImageName] forState:UIControlStateNormal];
}
- (void)setRightImageName:(NSString *)rightImageName {
    _rightImageName = rightImageName;
    [self.rightBtn setImage:[UIImage imageNamed:rightImageName] forState:UIControlStateNormal];
    if ([SettingObject objectIsValid:rightImageName]) {
        CGRect frame = self.rightBtn.frame;
        frame.size.width = 25;
        frame.size.height = 25;
        self.rightBtn.frame = frame;
    }
}

#pragma mark - ViewSignalProtocol

- (RACSignal *)getSignalWithDictionary:(NSDictionary *)dict {
    NSString *str = dict[@"action"];
    if (!str)   return [RACSignal empty];
    
    if ([str isEqualToString:@"left"]) {            // 左
        return [self.leftBtn rac_signalForControlEvents:UIControlEventTouchUpInside];;
    } else if ([str isEqualToString:@"right"]) {     // 右
        return [self.rightBtn rac_signalForControlEvents:UIControlEventTouchUpInside];;
    } else {
        return [RACSignal empty];
    }
}

#pragma mark - getter

- (UIButton *)leftBtn {
    if (_leftBtn == nil) {
        _leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, kStatusBarHeight, 44, 44)];
        [self addSubview:_leftBtn];
    }
    return _leftBtn;
}

- (UIButton *)rightBtn {
    if (_rightBtn == nil) {
//        _rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 60, 20, 44, 44)];
        _rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 40, 10+kStatusBarHeight, 25, 25)];
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:_rightBtn];
    }
    return _rightBtn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
